#!/usr/bin/env python 
# -*- coding:utf-8 -*-

import os  # 操作系统的模块
from io import StringIO
import json # json模块
import pickle

class Student:
    aa = None
    def __init__(self, name, age, score):
        self.name = name
        self.age = age
        self.score = score


if __name__ == "__main__":


    # 只读方式打开文件，不存在则异常
    # with open("../../test.txt", "r", encoding='UTF-8') as f:
        # 读取全部
        # print(f.read())

    # 写入文件测试 w=新建写入；a=追加写入；
    with open("../../test.txt", mode='a', encoding='utf-8') as f2:

        f2.writelines("你好\n" )
        f2.writelines("0123456789abcde\n")

        # f2.write("hahahah123")
        # f2.write("hahaadshah123345345")


    # 内存中读写信息
    # f = StringIO()
    # f.write("12345")
    # print(f.tell())
    # # f.seek(0) # 重置指针位置，来读取内容
    # print(f.read())
    # print(f.tell())


    # 操作系统名称
    print("os.getcwd = %s" % os.getcwd())
    print("os.path.abspath('.') = %s" % os.path.abspath('.'))
    print("os.name = %s"%os.name)
    print("os.environ = %s"%os.environ)
    print("os._exists = %s"%os.path.exists("/Users/kunda-liang/Desktop/music"))
    # os.mkdir('/Users/kunda-liang/Desktop/music1')
    print("")

    # 序列化
    d = dict(name='Bob', age=20, score=88)
    dumps = pickle.dumps(d)
    print("序列化 pickle.dumps(d) = %s" % dumps)

    # 反序列化
    dd = pickle.loads(dumps)
    print("反序列化 pickle.loads(dumps) = %s" % dd)


    s = Student('Bob', 20, 88)
    s.aa = "aaa"
    json_stu1 = json.dumps(s, default=lambda obj: obj.__dict__)
    # json_stu2 = json.dumps(s)
    print("json 序列化1 = {}".format(json_stu1))
    # print("json 序列化2 = {}".format(json_stu2))


    j = "{\"data\":[\"1\",\"2\"]}"
    jsona = json.loads(j)
    print("json 反序列化 = {}".format(jsona['data']))
    jsona['data1'] = "aaa"
    print("json 反序列化 = {}".format(jsona))

